/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Windows;
import CodeGenerator.GenerateXMLFromCode;
import Data.MyClipBoard;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author DavidUser
 */
public class XMLGenerator extends javax.swing.JFrame {

    /**
     * Creates new form XMLGenerator
     */
    public XMLGenerator() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tf_Name = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        ta_CodeInput = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        b_GenerateCode = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        cb_Language = new javax.swing.JComboBox();
        b_PasteClipBoard = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        tf_Tag = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        ta_ReplaceableCode = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        ta_CodeOutput = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        b_SaveGeneratedCode = new javax.swing.JToggleButton();
        b_CloseWithoutSaving = new javax.swing.JToggleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Input"));

        jLabel1.setText("Enter name of code");

        ta_CodeInput.setColumns(20);
        ta_CodeInput.setRows(5);
        ta_CodeInput.setTabSize(3);
        ta_CodeInput.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ta_CodeInputMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(ta_CodeInput);

        jLabel2.setText("Code you wish to transform");

        b_GenerateCode.setText("Generate code");
        b_GenerateCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_GenerateCodeActionPerformed(evt);
            }
        });

        jLabel5.setText("Language of code");

        cb_Language.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "C", "C#", "C++/MFC", "Java" }));
        cb_Language.setSelectedIndex(2);
        cb_Language.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_LanguageActionPerformed(evt);
            }
        });

        b_PasteClipBoard.setText("Paste from clipboard");
        b_PasteClipBoard.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_PasteClipBoardActionPerformed(evt);
            }
        });

        jLabel6.setText("Tag");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(cb_Language, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tf_Tag))
                    .addComponent(tf_Name))
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(b_GenerateCode, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(b_PasteClipBoard, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
            .addComponent(jScrollPane1)
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {b_GenerateCode, b_PasteClipBoard});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_Name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(3, 3, 3)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(cb_Language, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(tf_Tag, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(b_GenerateCode, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(b_PasteClipBoard)))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Output"));
        jPanel2.setToolTipText("");

        ta_ReplaceableCode.setColumns(20);
        ta_ReplaceableCode.setEditable(false);
        ta_ReplaceableCode.setRows(4);
        jScrollPane2.setViewportView(ta_ReplaceableCode);

        jLabel3.setText("Replaceable codes");

        ta_CodeOutput.setColumns(20);
        ta_CodeOutput.setEditable(false);
        ta_CodeOutput.setRows(5);
        ta_CodeOutput.setTabSize(3);
        jScrollPane3.setViewportView(ta_CodeOutput);

        jLabel4.setText("Generated code");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3)
            .addComponent(jScrollPane2)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addGap(7, 7, 7)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
                .addContainerGap())
        );

        b_SaveGeneratedCode.setText("Save");

        b_CloseWithoutSaving.setText("Close without saving");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(b_SaveGeneratedCode, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(b_CloseWithoutSaving, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {b_CloseWithoutSaving, b_SaveGeneratedCode});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(b_SaveGeneratedCode)
                    .addComponent(b_CloseWithoutSaving))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void b_GenerateCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_GenerateCodeActionPerformed
        // TODO add your handling code here:
        GenerateXMLFromCode xmlCode = new GenerateXMLFromCode();
        xmlCode.SetName(tf_Name.getText());
        xmlCode.SetTag(this.tf_Tag.getText());
        xmlCode.SetLanguage((String)cb_Language.getSelectedItem());
        xmlCode.SetInputText(ta_CodeInput.getText());
        if (!xmlCode.GetError()){
            ta_ReplaceableCode.setText(xmlCode.GetParams());
            ta_CodeOutput.setText(xmlCode.GetGeneratedText());
        }
        else 
            ta_ReplaceableCode.setText("Error with parameters select!" +"\r\n" + xmlCode.GetErrorString());
    }//GEN-LAST:event_b_GenerateCodeActionPerformed

    private void cb_LanguageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_LanguageActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cb_LanguageActionPerformed

    private void b_PasteClipBoardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_PasteClipBoardActionPerformed
        // TODO add your handling code here:
        ta_ReplaceableCode.setText("");
        ta_CodeOutput.setText("");
        MyClipBoard clip = new MyClipBoard();
        try {
            String x = clip.GetClipBoard();
        } catch (IOException ex) {
            Logger.getLogger(XMLGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            ta_CodeInput.setText(clip.GetClipBoard());
        } catch (IOException ex) {
            Logger.getLogger(XMLGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_b_PasteClipBoardActionPerformed

    private void ta_CodeInputMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ta_CodeInputMouseClicked
        // TODO add your handling code here:
        if (evt.getButton() == MouseEvent.BUTTON3){
            PopUpPaste popUp = new PopUpPaste(ta_CodeInput,ta_ReplaceableCode, ta_CodeOutput);
            popUp.show(evt.getComponent(), evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_ta_CodeInputMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(XMLGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(XMLGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(XMLGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(XMLGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new XMLGenerator().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton b_CloseWithoutSaving;
    private javax.swing.JButton b_GenerateCode;
    private javax.swing.JButton b_PasteClipBoard;
    private javax.swing.JToggleButton b_SaveGeneratedCode;
    private javax.swing.JComboBox cb_Language;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea ta_CodeInput;
    private javax.swing.JTextArea ta_CodeOutput;
    private javax.swing.JTextArea ta_ReplaceableCode;
    private javax.swing.JTextField tf_Name;
    private javax.swing.JTextField tf_Tag;
    // End of variables declaration//GEN-END:variables
}
